package com.coffee.controller;

import com.coffee.conmmon.BaseResult;
import com.coffee.entity.Cart;
import com.coffee.entity.Cart1;
import com.coffee.entity.User;
import com.coffee.service.impl.CartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/data")
@CrossOrigin
public class CartController {
    @Autowired
    private CartServiceImpl cartService;
   @RequestMapping(value = "/listarr",method = RequestMethod.POST)
    public BaseResult findAllCart1(HttpServletRequest request){
       HttpSession session = request.getSession();
       Object user = session.getAttribute("user");
       User user1=null;
       if(user!=null&&user instanceof User){
           user1=(User)user;
       }
       List<Cart1> allCart = cartService.findAllCart1(user1.getUid());
       BaseResult baseResult = new BaseResult();
       baseResult.setCode(200);
       baseResult.setMessage("查询成功");
       baseResult.setData(allCart);
       return baseResult;
   }
   @RequestMapping(value = "/countarr",method = RequestMethod.POST)
    public BaseResult updateGood(@RequestBody Map map,HttpServletRequest request){
       HttpSession session = request.getSession();
       Object user = session.getAttribute("user");
       User user1=null;
       if(user!=null&&user instanceof User){
           user1=(User)user;
       }
       Integer coid =(Integer)map.get("id");
       Integer type=(Integer)map.get("type");
       Integer num=0;
       Cart cart = cartService.findByIdCart(coid,user1.getUid());
       if(type==0) {
           if (cart.getConumber() == 1) {
               num = cartService.deleteCart(cart.getCaid(),user1.getUid());
           } else {
               Cart cart1 = new Cart();
               cart1.setUid(user1.getUid());
               cart1.setCaid(cart.getCaid());
               cart1.setConumber(cart.getConumber() - 1);
               num = cartService.updateCart(cart1);
           }
       }else {
               Cart cart1 = new Cart();
               cart1.setUid(user1.getUid());
               cart1.setCaid(cart.getCaid());
               cart1.setConumber(cart.getConumber() + 1);
               num = cartService.updateCart(cart1);
           }
       if(num==1) {
           BaseResult baseResult = new BaseResult();
           baseResult.setCode(200);
           baseResult.setMessage("操作成功");
           return baseResult;
       }else {
           BaseResult baseResult = new BaseResult();
           baseResult.setCode(300);
           baseResult.setMessage("操作失败");
           return baseResult;
       }

    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public BaseResult insertCart(@RequestBody Map map, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        User user1=null;
        if(user!=null&&user instanceof User){
            user1=(User)user;
        }
        Integer coid =(Integer)map.get("id");
        Cart cart = cartService.findByIdCart(coid,user1.getUid());
        Integer conumber=0;
        Integer num=0;
        if(cart!=null){
            conumber=cart.getConumber()+1;
            Cart cart1 = new Cart();
            cart1.setCaid(cart.getCaid());
            cart1.setUid(user1.getUid());
            cart1.setCoid(coid);
            cart1.setConumber(conumber);
            num=cartService.updateCart(cart1);
        }else{
            Cart cart1 = new Cart();
            cart1.setUid(user1.getUid());
            cart1.setCoid(coid);
            cart1.setConumber(1);
            num=cartService.insertCart(cart1);
        }
        if(num==1) {
            BaseResult baseResult = new BaseResult();
            baseResult.setCode(200);
            baseResult.setMessage("添加购物车成功");
            return baseResult;
        }else {
            BaseResult baseResult = new BaseResult();
            baseResult.setCode(300);
            baseResult.setMessage("添加购物车失败");
            return baseResult;
        }
    }
    @RequestMapping(value = "/deleteAll",method = RequestMethod.POST)
    public BaseResult deleteAll(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        User user1=null;
        if(user!=null&&user instanceof User){
            user1=(User)user;
        }
        Integer integer = cartService.deleteAll(user1.getUid());
        if(integer>0){
            BaseResult baseResult = new BaseResult();
            baseResult.setCode(200);
            baseResult.setMessage("购买成功");
            return baseResult;
        }else{
            BaseResult baseResult = new BaseResult();
            baseResult.setCode(300);
            baseResult.setMessage("购买失败");
            return baseResult;
        }
    }

}
